<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增监听器
            </el-header>
            <el-main>
                <el-form ref="addMonitData" :rules="rules" :model="addMonitData" label-width="115px"
                         label-position="left">
                    <el-form-item label="监听器名称 :" prop="name">
                        <el-input v-model="addMonitData.name" placeholder="请输入监听器名称"></el-input>
                    </el-form-item>
                    <el-form-item label="监听器内容 :" prop="scp">
                        <el-input type="textarea" v-model="addMonitData.hitScp" placeholder="请输入监听器内容" :rows="6"></el-input>
                    </el-form-item>
                    <el-form-item label="执行器名称 :" prop="actorName">
                        <el-select v-model="addMonitData.actorName" filterable placeholder="请选择" style="width: 100%;" @change="changeValue">
                            <el-option
                                    v-for="item in actorOptions"
                                    :key="item.uuid"
                                    :label="item.name"
                                    :value="item.name">
                                {{item.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="执行器内容 :" prop="actorScp">
                        <el-input readonly type="textarea" v-model="addMonitData.actorScp" :rows="6"></el-input>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addMonit",
        data() {
            return {
                uuid: '',
                addMonitData: {
                    name: '',
                    hitScp:'',
                    actorName:'',
                    actorUuid:'',
                    actorScp:''
                },
                actorOptions:[],
                rules: {
                    name: [
                        {required: true, message: '监听器名称不能为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        mounted() {
            this.queryActor();
        },
        methods: {
            //查询执行器数据
            queryActor() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/actor/search/findAll",
                    params: {}
                }).then(response => {
                    this.actorOptions = response.data.content;
                }).catch(error => {
                    console.log(error);
                });
            },
            changeValue(){
                this.actorOptions.forEach( list =>{
                    if(list.name === this.addMonitData.actorName){
                        this.addMonitData.actorName = list.name
                        this.addMonitData.actorUuid = list.uuid
                        this.addMonitData.actorScp = list.scp
                    }
                })

            },
            submit() {
                this.$refs['addMonitData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.addMonitData = replaceSpacesObj(this.addMonitData);
                    this.axios.request({
                        method: "post",
                        url: "./smt/kdi/monit",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: {
                            name: this.addMonitData.name,
                            hitScp: this.addMonitData.hitScp,
                            actorName: this.addMonitData.actorName,
                            actorUuid: this.addMonitData.actorUuid,
                            actorScp: this.addMonitData.actorScp,
                        }
                    }).then(() => {
                        this.endProcessing();
                    }).catch(error => {
                        console.log(error);
                        this.info = '监听器新增失败';
                        this.infoShow = true;
                    });
                });
            },
            endProcessing() {
                this.info = this.info + '新增成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanMonit();
                    this.info = '';
                }, 2000);
            },
            cleanMonit() {
                this.addMonitData = {
                    name: '',
                    hitScp:'',
                    actorName:'',
                    actorUuid:'',
                    actorScp:''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanMonit();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 900px;
        height: 100%;
        z-index: 100;
    }


</style>